package org.kommii.mongodb;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.kommii.common_utils.RandomInt_generator;
import com.mongodb.MongoClient;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;

public class Mongodb_ConnPools {
	
	private static List<String> dbServerIP;
	private static List<Integer> dbServerPort;
    private static List<String> dbName;
    private static List<Integer> connSize;
    private static List<String> dbUserName;
    private static List<String> dbPassword;

    private static List<Integer> connectionsPerHost;
    private static List<Integer> threadsAllowedToBlockForConnectionMultiplier;
    private static List<Integer> connectTimeout;
    private static List<Boolean> autoConnectRetry;
    
    private static Map<String,Object> map = new HashMap<String,Object>();
	static {
		
		dbServerIP = MongoDb_Config.getDbServerIP();
		dbServerPort = MongoDb_Config.getDbServerPort();
		
		dbName = MongoDb_Config.getDbName();
		dbUserName = MongoDb_Config.getDbUserName();
		connSize = MongoDb_Config.getConnSize();
		dbPassword = MongoDb_Config.getDbPassword();
		
		connectionsPerHost = MongoDb_Config.getConnectionsPerHost();
		threadsAllowedToBlockForConnectionMultiplier = MongoDb_Config.getThreadsAllowedToBlockForConnectionMultiplier();
		connectTimeout = MongoDb_Config.getConnectTimeout();
		autoConnectRetry = MongoDb_Config.isAutoConnectRetry();
		
		 try {
			 int size = dbServerIP.size();
			 for(int i=0;i<size;i++){
				 DB[] DBArry = new DB[connSize.get(i)] ;
				 for (int j = 0; j < connSize.get(i); j++) {
						Mongo m = new Mongo(dbServerIP.get(i), dbServerPort.get(i));
						MongoOptions opt = m.getMongoOptions();
						opt.setMaxWaitTime(30000);
						opt.setConnectionsPerHost(connectionsPerHost.get(i));
						opt.setThreadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier.get(i));
						opt.setAutoConnectRetry(autoConnectRetry.get(i));
						opt.setConnectTimeout(connectTimeout.get(i));
						//m.setOptions(1);
						DBArry[j] = m.getDB(dbName.get(i));
				}
				 map.put(dbName.get(i), DBArry);
				 map.put("name_"+dbName.get(i), dbUserName.get(i));
				 map.put("pw_"+dbName.get(i), dbPassword.get(i));
				 System.out.println("init mongodb conn pool "+dbName.get(i)+" : " + connSize.get(i)*10);
			 }
	           
	        
	        } catch (UnknownHostException e) {
	            throw new RuntimeException(e);
	        }
	}
	
	/**
	 * 获取mongodb数据库中的某个集合
	 * @param dbname 数据库名 coll_name 集合名
	 * @return 返回某个集合的mongdb连接
	 */
	public static DBCollection getMongoDB_Coll(String dbname,String coll_name){
		DB[] DBArray  = (DB[])map.get(dbname);
		String uname = (String)map.get("name_"+dbname);
		String pw = (String)map.get("pw_"+dbname);
		boolean b = false;
		int i = RandomInt_generator.generatorInt(DBArray.length);
		if(null!=uname && null!=pw && !"".equals(pw.trim()) && !"".equals(uname.trim())){
			b = DBArray[i].authenticate(uname, pw.toCharArray());
			if(b)
				return DBArray[i].getCollection(coll_name);
			else
				return null;
		}else{
			return DBArray[i].getCollection(coll_name);
		}	
	}
	
	public static void showConnPoolStatus(){
		System.out.println("连接池共连接 "+dbServerIP.size()+" 个数据库:");
		System.out.println("####################################");
		for(int i=0;i<dbServerIP.size();i++){
			System.out.println("第 "+(i+1)+" 个:");
			System.out.println("ip: "+dbServerIP.get(i));
			System.out.println("port: "+dbServerPort.get(i));
			System.out.println("dbName: "+dbName.get(i));
			System.out.println("conn pool size: "+connSize.get(i)*10);
			System.out.println("connectTimeout: "+connectTimeout.get(i));
			System.out.println("autoRetry: "+autoConnectRetry.get(i));
			System.out.println("####################################");
		}
	}

	
}
